package br.com.portadelivery.app.views;

import br.com.portadelivery.app.R;
import br.com.portadelivery.app.interfaces.IServer;
import br.com.portadelivery.app.model.Usuario;
import br.com.portadelivery.app.server.UsuarioServer;
import br.com.portadelivery.app.webservices.UsuarioREST;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class UsuarioActivity extends BaseActivity {

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch(v.getId()){
		case R.id.buttonHomeSalvar:
			SalvarClick();
			setClear();
			break;
		case R.id.buttonHomeLimpar:
			setClear();
			break;
		case R.id.buttonHomeFechar:
			finish();
			break;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}


	public void DoLoad(int RLayoutID, String tag) {
		// TODO Auto-generated method stub
		super.DoLoad(R.layout.cadastrousuario, "UsuarioActivity");
		
		bSalvar = (Button)findViewById(R.id.buttonHomeSalvar);
		bLimpar = (Button)findViewById(R.id.buttonHomeLimpar);
		bSair = (Button)findViewById(R.id.buttonHomeFechar);
		tEmail = (EditText)findViewById(R.id.editTextHomeEmail); 
		tNome = (EditText)findViewById(R.id.editTextHomeNome);
		tSenha= (EditText)findViewById(R.id.editTextHomeSenha);
		tSobreNome= (EditText)findViewById(R.id.editTextHomeSobreNome);
		
		bSalvar.setOnClickListener(this);
		bLimpar.setOnClickListener(this);
		bSair.setOnClickListener(this);
		
		setCampos();
		setClear();
	}
	
	Button bSalvar;
	Button bLimpar;
	Button bSair;
	EditText tEmail;
	EditText tNome;
	EditText tSenha;
	EditText tSobreNome;	
	
	private String email;
	private String nome;
	private String senha;
	private String sobrenome;
	private Usuario dataset;
	
	private IServer server = new UsuarioServer();
	
	private void setCampos(){
		 email = tEmail.getText().toString();
		 nome = tNome.getText().toString();
		 senha = tSenha.getText().toString();
		 sobrenome = tSobreNome.getText().toString();
	}
	private void setClear(){
		tEmail.setText("");
		tNome.setText("");
		tSenha.setText("");
		tSobreNome.setText("");
	}
	private void SalvarClick(){
		setCampos();
		dataset = new Usuario(nome, sobrenome, email, senha,-1);
		UsuarioREST server = new UsuarioREST();
		try {
			server.inserir(dataset);
			mensagem("Usuario inserido com sucesso");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.e(getTAG(),"SalvarClick: ",e);
		}
	}
}
